define(function(require, exports, module) {

	var $ = require('jquery');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('jbox');
	var List = require('../util/list');

	exports.options = {
		listLink : '',
		listParams : '',
		pageLink : '',
		pageParams : ''
	};
	var id = "1";
	var s = "8";
	var flag = '';
	var key = '';
	var uid = $g_user.login;
	var p_status = '5';
	
	if ($g_user.login == 'true') {
		var uid = '' + $g_user.id;
		if ($g_user.role == 'admin') {
			var p_status = '1';
		} else {
			var p_status = '5';
		}
		// //console.log($g_user.role+":"+p_status);
	} else {
		uid = '-1';
		var p_status = '5';
	}


	//加载标签
	var url = $CONFIG.base_url + '/api/configure/get';
	$.get(url,function(data){
		$("#itemFirstname").text(data.itemFirstname);
		$("#itemSecname").text(data.itemSecname);
		$("#itemFname").text(data.itemFname);
	});

	// 项目分类加载
	exports.options.listLink = $CONFIG.base_url + "/api/category";
	var categoryList = $.get(exports.options.listLink);
	categoryList.done(function(data) {
		var selectKind = template.render('peojectFl-template', data);
		$('#type-nav').empty().append(selectKind);
	});

	// 执行区域（省）加载
	exports.getRegions=function(url,params,o){
		exports.options.regionLink = $CONFIG.base_url + url;
		exports.options.regionParams = params;
		var region = $.get(exports.options.regionLink, exports.options.regionParams);
		region.done(function(data) {
			var regionlist = template.render("regions-template", data);
			$('#'+o).empty().append(regionlist);
		});
	};
	exports.getRegions("/api/region/find/level",{
		"level" : 1,
		"p" : 1,
		"s" : 7
	},"regions");
	
	exports.getRegions("/api/region/list/level",{
		"level" : 1,
		"flag":0
	},"regions_all");
	$("#more-region").on("click",function(){
		$("#regions").slideToggle(0);
		$("#regions_all").slideToggle(0);
		$(this).hide();
		$("#less-region").show();
	});
	$("#regions_all").on('click', 'a', function() {
		if($(this).attr("id")=="region_all"){
			s = "8";
			exports.loadAll(p_status, function() {});
		}else{
			$("#city").css("display","block");
			s = "8";
			id = $(this).attr("data");
			exports.loadCity(id);
		}
	});
	$("#less-region").on("click",function(){
		$("#regions").slideToggle(0);
		$("#regions_all").slideToggle(0);
		$(this).hide();
		$("#more-region").show();
	});
	
	//数字过大转换为“xx万”的形式
	exports.turn = function(){
		$("span[id=attentioncount]").each(function(){
			var val = $.trim($(this).text());
			if(val >= 1000000){
				val = val/10000;
				val = Math.round(val)
				$(this).empty().append("<b>"+val+"万</b>")
			}
			else if(val >= 100000){
				val = val/1000;
				val = Math.round(val);
				val = val/10;
				$(this).empty().append("<b>"+val+"万</b>")
			}
			else if(val >= 10000){
				val = val/100;
				val = Math.round(val);
				val = val/100;
				$(this).empty().append("<b>"+val+"万</b>")
			}else{
				$(this).empty().append("<b>"+val+"</b>")
			}
		})
		$("span[id=fundcurrent]").each(function(){
			var val = $.trim($(this).text());
			if(val >= 1000000){
				val = val/10000;
				val = Math.round(val)
				$(this).empty().append("<b>"+val+"万</b>")
			}
			else if(val >= 100000){
				val = val/1000;
				val = Math.round(val);
				val = val/10;
				$(this).empty().append("<b>"+val+"万</b>")
			}
			else if(val >= 10000){
				val = val/100;
				val = Math.round(val);
				val = val/100;
				$(this).empty().append("<b>"+val+"万</b>")
			}else{
				$(this).empty().append("<b>"+val+"</b>")
			}
		})
		$("span[id=productamount]").each(function(){
			var val = $.trim($(this).text());
			if(val >= 1000000){
				val = val/10000;
				val = Math.round(val)
				$(this).empty().append("<b>"+val+"万</b>")
			}
			else if(val >= 100000){
				val = val/1000;
				val = Math.round(val);
				val = val/10;
				$(this).empty().append("<b>"+val+"万</b>")
			}
			else if(val >= 10000){
				val = val/100;
				val = Math.round(val);
				val = val/100;
				$(this).empty().append("<b>"+val+"万</b>")
			}else{
				$(this).empty().append("<b>"+val+"</b>")
			}
		})
	}
	
	// 加载全部
	exports.loadAll = function(e, callback) {
		var loadAll = new List();
		loadAll.options.template = "project-template";
		loadAll.options.container = "#projects";
		loadAll.options.links = {
			"list" : $CONFIG.base_url + "/api/project/list/all"
		};
		loadAll.options.params = {
			"status" : e,
			"s" : s
		};
		loadAll.load(function() {
			exports.turn();
			if (typeof (callback) == "function") {
				callback();
			}
		});
	};
	
	// 加载搜索结果
	exports.searchAll = function(e, callback) {
		var loadAll = new List();
		loadAll.options.template = "project-template";
		loadAll.options.container = "#projects";
		loadAll.options.links = {
			"list" : $CONFIG.base_url + "/api/project/list/all"
		};
		loadAll.options.params = {
			"status" : '5',
			"qs" : e,
			"s" : s
		};
		loadAll.load(function() {
			exports.turn();
			if (typeof (callback) == "function") {
				callback();
			}
		});
	};

	// 项目状态筛选
	exports.loadStatus = function(e, callback) {
		var loadStatus = new List();
		loadStatus.options.template = "project-template";
		loadStatus.options.container = "#projects";
		loadStatus.options.links = {
			"list" : $CONFIG.base_url + "/api/project/list"
		};
		loadStatus.options.params = {
			"status" : e,
			"s" : s
		};
		loadStatus.load(function() {
			exports.turn();
			if (typeof (callback) == "function") {
				callback();
			}
		});
	};

	// 点击根据状态筛选项目
	$(".status-box").on('click', 'a', function() {
		s = "8";
		da = $(this).attr("data");
		pro_status = da;
		exports.loadStatus(da);
		$(".status-box a").removeClass('active');
		$(this).addClass('active');
	});

	//动态加载项目收益类型
	var getting = $.get($CONFIG.base_url + "/api/system/project/categorylist",{"type":3});
	getting.done(function(data) {
		data={"data":data};
		var html = template.render("profit-template", data);
		$("#profit").empty().append(html);
		$("#profit").prepend('<a data="0" class="profitall active" style="margin-left:0;">收益类型</a>');
		
	});

	// 项目分类筛选
	exports.loadKinds = function(e, callback) {
		var loadKinds = new List();
		loadKinds.options.template = "project-template";
		loadKinds.options.container = "#projects";
		loadKinds.options.links = {
			"list" : $CONFIG.base_url + "/api/category/project"
		};
		loadKinds.options.params = {
			"cid" : e,
			"s" : s,
			"status":'5'
		};
		loadKinds.load(function() {
			exports.turn();
			if (typeof (callback) == "function") {
				callback(1);
			}
		});
	};
	//收益项目筛选
	exports.profitKinds=function(e,callback){
		var profitKinds=new List();
		profitKinds.options.template="project-template";
		profitKinds.options.container="#projects";
		profitKinds.options.links={"list":$CONFIG.base_url+"/api/project/list/by/benefit"};
		profitKinds.options.params={
				"benefitId":e,
				"s":s
		};
		profitKinds.load(function(){
			exports.turn();
			if(typeof(callback)=="function"){
				callback();
			};
		});
	};
	
	// 点击项目分类筛选
	$("#type-nav").on('click', 'a', function() {
		s = "8";
		cid = $(this).attr("data");
		exports.loadKinds(cid, function() {
			// $("#projects").on("click", "#more", function() {
			// s = parseInt(s) + parseInt(8);
			// exports.loadKinds(cid, s);
			// });
	});
		$("#type-nav a").removeClass('active');
		$(this).addClass('active');
	});

	//点击收益分类筛选
	$("#profit").on('click', 'a', function(e,callback) {
		e= $(this).attr("data");
		exports.profitKinds(e, function() {
	});
		$("#profit a").removeClass('active');
		$(this).addClass('active');
	});
	
	// 根据行政区域筛选
	exports.loadRegon = function(e) {
		var loadRegon = new List();
		loadRegon.options.template = "project-template";
		loadRegon.options.container = "#projects";
		loadRegon.options.links = {
			"list" : $CONFIG.base_url + "/api/project/region"
		};
		loadRegon.options.params = {
			"rid" : e,
			"s" : s,
			"status":'5'
		};
		loadRegon.load(function(){
			exports.turn();
		});
	};
	exports.loadCity = function(parentId) {
		exports.options.loadCityLink=$CONFIG.base_url + "/api/region/list/city";
		exports.options.loadCityParams={
			"parentId":parentId
		};
		var loadList=$.get(exports.options.loadCityLink,exports.options.loadCityParams);
		loadList.done(function(data){
			var loadStr=template.render("cities-template",data);
			$("#cities").empty().append(loadStr);
			$("#city255448").modal();
			$("#cities").on("click","a",function(){
				$("#city255448").modal('hide');
				var id=$(this).attr("data");
				exports.loadRegon(id);
			});
		});
	};
	// 点击根据项目区域筛选
	$("#regions").on('click', 'a[id!="region_all"]', function() {
		$("#city").css("display","block");
		s = "8";
		id = $(this).attr("data");
		exports.loadCity(id);
	});
	$("#regions").on('click', '#region_all', function() {
		s = "8";
		exports.loadAll(p_status, function() {
			// $("#projects").on("click", "#more", function() {
			// s = parseInt(s) + parseInt(8);
			// exports.loadKinds(id, s);
			// });
	});
	});
	// 根据项目排序筛选
	exports.loadSort = function(by, callback) {
		var loadSort = new List();
		loadSort.options.template = "project-template";
		loadSort.options.container = "#projects";
		loadSort.options.links = {
			"list" : $CONFIG.base_url + "/api/project/sort/" + by
		};
		loadSort.options.params = {
			"s" : s,
			"status" : '5'
		};
		loadSort.load(function() {
			exports.turn();
			if (typeof (callback) == "function") {
				callback();
			}
		});
	};
	
	//根据tags筛选
	exports.loadtags=function(e){
		var hotProject = new List();
		hotProject.options.template = "project-template";
		hotProject.options.container = "#projects";
		hotProject.options.links = {"list" : $CONFIG.base_url + "/api/project/tags"};
		hotProject.options.params = {"s" : s,"tags" : e};
		hotProject.load(function(){
			exports.turn();
		});
	};
	
	// 点击项目排序筛选
	$('.sort-box select').change(function() {
		s = "8";
		var by = $(this).children('option:selected').attr("data");
		
		/*if(by == "3" || by == "4") {
			key = $(this).children('option:selected').attr("key");
			flag = by;
			exports.loadStatus(flag);*/
		if(by == "tags"){
			key = $(this).children('option:selected').attr("key");
			flag = $(this).children('option:selected').attr("tags");
			console.log(flag);
			exports.loadtags(flag);
		}else
		exports.loadSort(by);
	});
	// $(".need_support").click(function() {
	// 	s = "8";
	// 	key = $(this).attr("key");
	// 	flag = $(this).attr("data");
	// 	exports.loadStatus("3");
	// });
	// $(".need_attention").click(function() {
	// 	s = "8";
	// 	key = $(this).attr("key");
	// 	flag = $(this).attr("data");
	// 	exports.loadStatus("4");
	// });
	$(".load_all").click(function() {
		s = "8";
		exports.loadAll(p_status);
	});
	
	//获取页面地址参数
	exports.getParam = function(path,name) {
		var value = path.substr(path.lastIndexOf('?') + 1);
		var  reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)","i");
		var param = value.match(reg);
		if (param != null) return param[2]; 
		return null;

		
	};
	
	// 转跳页面定位
	var path = decodeURIComponent(window.location.href);
	status = path.substr(path.lastIndexOf('?') + 1).charAt(0);
	//var value = path.substr(path.lastIndexOf('=') + 1);
	var value = exports.getParam(path,status);
	console.log(status+":"+value);
	var qs = exports.getParam(path,"qs");
	if(qs !== null){
		exports.searchAll(qs, function() {
			key = "1";
		});
	}else if(isNaN(value)||value == null){
		exports.loadAll(p_status, function() {
			key = "1";
		});
	}else{
		flag=value;
		switch (status) {
			case "s":
			$("#project-kind-list a").removeClass('discover-select');
			$("#" + value).addClass('discover-select');
			$("#" + value).addClass('active');
			exports.loadStatus(value);
			key="2";
			break;
			case "c":
			$("#project-kind-list a").removeClass('discover-select');
			$("#kind" + value).addClass('discover-select');
			$("#kind" + value).parent().children().removeClass('active');
			$("#kind" + value).addClass('active');
			$.get(exports.options.regionLink, exports.options.regionParams).done(function() {
				$("#kind" + value).addClass('discover-select');
				exports.loadKinds(value);
			});
			
			key="3";
			break;
			case "p":
			$("#project-kind-list a").removeClass('discover-select');
			$("#" + value).addClass('discover-select');
			exports.loadStatus(value);
			break;
			case "t":
			$(".sort-box .sort"+value).attr("selected",true);
			$('.sort-box select').change();
			break;
			case "a":
			$("#project-kind-list a").removeClass('discover-select');
			$(".need_attention").addClass('discover-select');
			$(".need_attention").click();
			break;
			default:
		}
	}

	// 筛选栏选中事件
	$('#project-kind-list').on('click', 'a', function() {
		$("#project-kind-list a").removeClass('discover-select');
		$(this).addClass('discover-select');
	});
	// 回到顶端按钮
	$(window).scroll(function() {
		var scrollTop = $(document).scrollTop();
		if (scrollTop > 0) {
			$(".back_top").show();
		} else {
			$(".back_top").hide();
		}
	});

	$("#project-kind-list").on("click", "a", function() {
		key = $(this).attr("key");
		flag = $(this).attr("data");
	});
	// 加载更多
	$("#projects").on("click", "#more", function() {
		s = parseInt(s) + parseInt(8);
		switch (key) {
		case "1":
			exports.loadAll(p_status, s);
			break;
		case "2":// 根据项目状态加载更多
			exports.loadStatus(flag, s);
			break;
		case "3":// 根据项目分类加载更多
			exports.loadKinds(flag, s);
			break;
		case "4":// 根据区域加载更多
			exports.loadRegon(flag, s);
			break;
		case "5":// 根据项目排序加载更多
			exports.loadSort(flag, s);
			break;
		case "6":// 根据最需资助最需关注加载更多
			exports.loadStatus(flag, s);
			break;
		case "7"://根据tags加载更多
			exports.loadtags(flag, s);
			break;
	}
});

});
